import pymysql

def conn_mysql(sql):
    dbparam = {
        'host': '127.0.0.1',
        # 'host': '10.10.10.180',
        'port': 3306,
        'user': 'root',
        'password': '1024',
        'database': 'alerts',
        'charset': 'utf8'
    }
    conn = pymysql.connect(**dbparam)
    cursor = conn.cursor()

    try:
        # sql = "SELECT DISTINCT county,point from aaa_yy1170 where belong like '%娄底%'"
        cursor.execute(sql)
        res = cursor.fetchall()
    except Exception as e:
        print('入库失败', e)
        conn.rollback()
    finally:
        cursor.close()
        conn.close()
        return res

if __name__ == '__main__':
    sql = "SELECT distinct county,point from alerts1170 where belong like '%常德1170%'"
    # sql = 'SELECT county,red_alerts,orange_alerts,yellow_alerts,blue_alerts from spider1170'
    x = conn_mysql(sql)
    print(x)